﻿using System;

class GCDandLCM
{
    static void Main(string[] args)
    {
        Console.WriteLine("最大公约数(GCD)和最小公倍数(LCM)计算器");
        Console.WriteLine("======================================");

        while (true)
        {
            try
            {
                Console.Write("\n请输入第一个正整数m（输入0退出）：");
                int m = GetPositiveInteger();
                if (m == 0) break;

                Console.Write("请输入第二个正整数n：");
                int n = GetPositiveInteger();


                int gcd = CalculateGCD(m, n);
                int lcm = CalculateLCM(m, n, gcd);


                DisplayResults(m, n, gcd, lcm);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"错误：{ex.Message}");
            }
        }

        Console.WriteLine("程序结束！");
    }


    static int GetPositiveInteger()
    {
        while (true)
        {
            if (int.TryParse(Console.ReadLine(), out int number))
            {
                if (number >= 0) return number;
                Console.Write("请输入非负整数：");
            }
            else
            {
                Console.Write("请输入有效的整数：");
            }
        }
    }


    static int CalculateGCD(int a, int b)
    {
        if (b == 0)
            return a;
        return CalculateGCD(b, a % b);
    }


    static int CalculateLCM(int a, int b, int gcd)
    {
        // 公式：LCM(a, b) = (a * b) / GCD(a, b)
        return (a * b) / gcd;
    }


    static void DisplayResults(int m, int n, int gcd, int lcm)
    {
        Console.WriteLine("\n📊 计算结果：");
        Console.WriteLine("================");
        Console.WriteLine($"输入数字：m = {m}, n = {n}");
        Console.WriteLine($"最大公约数(GCD)：{gcd}");
        Console.WriteLine($"最小公倍数(LCM)：{lcm}");
        Console.WriteLine($"验证：{m} × {n} = {gcd} × {lcm} = {m * n}");



        static string GetPrimeFactors(int number)
        {
            if (number <= 1) return number.ToString();

            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            int temp = number;


            while (temp % 2 == 0)
            {
                sb.Append("2 × ");
                temp /= 2;
            }

            for (int i = 3; i <= temp; i += 2)
            {
                while (temp % i == 0)
                {
                    sb.Append($"{i} × ");
                    temp /= i;
                }
            }

            if (sb.Length > 0)
                return sb.ToString().TrimEnd('×', ' ');
            else
                return number.ToString();
        }
    }
}
